package com.base.controller;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.base.po.voip.SelfShop;
import com.base.po.voip.SelfShopCategory;
import com.base.po.voip.Shopinfoes;
import com.base.po.voip.ShopinfoesExample;
import com.base.po.voip.ShopinfoesExample.Criteria;
import com.base.po.voip.Users;
import com.base.service.SelfShopCategoryService;
import com.base.service.SelfShopService;
import com.base.service.ShopinfoService;
import com.base.service.UsersService;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * 自营商城分类
 * 
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/admin")
public class index_SelfShopCategory {
	@Autowired
	private SelfShopCategoryService selfShopCategoryService;
	@Autowired
	private SelfShopService selfShopService;
	@Autowired
	private UsersService usersService;

	// 自营商城分类首页
	@RequestMapping("/selfShopCategory")
	public ModelAndView index(HttpServletRequest request) throws Exception {
		// 返回ModelAndView
		ModelAndView modelAndView = new ModelAndView();
		String adminUserId = (String) request.getSession().getAttribute("adminUserId");
		Integer adminUsergrade = (Integer) request.getSession().getAttribute("adminUsergrade");
	
		Users users = usersService.selectByPrimaryKey(adminUserId);
		List<SelfShopCategory> selfShopCategory = null;
		List<SelfShop> selfShops = null;
		SelfShop selfShop = null;
		if(users.getRole() == 0 || users.getRole() == 1) {
			selfShopCategory = selfShopCategoryService.selectByLevel1(adminUserId,adminUserId);
			selfShops = selfShopService.selectBySelfShop(adminUserId, adminUserId);
			if(selfShops != null && selfShops.size() > 0) {
				selfShop = selfShops.get(0);
			}
		}
		if(users.getRole() == 2) {
			selfShopCategory = selfShopCategoryService.selectByLevel1(users.getParentid(),users.getParentid());
			selfShops = selfShopService.selectBySelfShop(users.getParentid(), users.getParentid());
			if(selfShops != null && selfShops.size() > 0) {
				selfShop = selfShops.get(0);
			}
		}
		
		request.setAttribute("selfShopCategory", selfShopCategory);
		request.setAttribute("selfShop", selfShop);
		request.setAttribute("adminUsergrade", adminUsergrade);
		// 设置页面进行跳转
		modelAndView.setViewName("jsp/index_SelfShopCategory");
		return modelAndView;
	}
	
	//查询下级分类
	@RequestMapping(value = "/nextCategory", method = { RequestMethod.POST, RequestMethod.GET })
	public @ResponseBody List<SelfShopCategory> nextCategory(HttpServletRequest request, HttpServletResponse response,
			String topid) throws Exception {
		// 返回ModelAndView
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=UTF-8");
		
		List<SelfShopCategory> nextCategorys = selfShopCategoryService.selectByTopid(topid);
		
		if(nextCategorys != null && nextCategorys.size() > 0) {
			
		}else {
			nextCategorys.add(new SelfShopCategory("0", "未查询到二级分类"));
		}
		
		return nextCategorys;
	}
}
